/*
 * @Author: 姜斌
 * @Date: 2024-05-05 08:59:08
 * @LastEditTime: 2024-05-17 07:10:13
 * @LastEditors: 姜斌
 * @Description: 添加描述信息
 * @FilePath: \be_quna\app.js
 */
const createError = require("http-errors");
const express = require("express");
const path = require("path");
const indexRouter = require("./routes/index");
const app = express();
// config
const config = require("./config");
const timeout = require("connect-timeout");
const cors = require("cors");
const bodyParser = require("body-parser");
const compression = require("compression");
// app.set("views", path.join(__dirname, "views"));

app.use('/', express.static('views'));

// app.use(logger("dev"));
app.use(config.setHttpLogger()); //日志
config.setLogger(); // 输出文件
app.use(express.json()); // json格式上传
app.use(express.urlencoded({ extended: false })); // 解析url
app.use(express.static(path.join(__dirname, "public"))); //静态css地址
app.use(express.static(path.join(__dirname, "log_box"))); //日志地址
// app.enable('trust proxy') //允许设置代理
app.use(timeout("80s")); //设置超时时间
app.use(
  cors({
    credentials: true,
    origin: true,
    allowedHeaders: [
      "Content-Type",
      "Authorization', 'Content-Length",
      "X-Requested-With",
    ],
    maxAge: 1728000,
    methods: ["GET", "POST", "OPTIONS", "PUT", "DELETE"],
  })
); //跨域
app.use(compression()); //启动Gzip压缩功能
app.use(bodyParser.json({ limit: "1mb" })); //解析json格式
app.use(bodyParser.urlencoded({ limit: "1mb", extended: false })); //解析form格式
app.use(bodyParser.raw({ limit: "100kb", type: "text/plain" })); //解析二进制格式
app.use(function (req, res, next) {
  // 超时处理
  if (!req.timedout) next();
});

app.use("/rxapi", indexRouter); //素材


// catch 404 and forward to error handler
app.use((req, res, next) => {
  next(createError(404));
});

// error handler 错误日志监听
app.use((err, req, res, next) => {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get("env") === "development" ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render("error");
});

module.exports = app;
